<template>
	<view class="fires">
		<view style="margin-top: 20rpx;">
			<fui-section title="监管报警媒体流地址" isLine>
				<template v-slot:right>
					<fui-icon @click="warning" :size="30" color="#FF2B2B" name="info"></fui-icon>
				</template>
			</fui-section>

			<fui-input v-if="!newlist.supervisionAlarmRtmp" v-model="supervisionAlarmRtmp" :padding="['20rpx','32rpx']"
				label="RTMP" :bottomLeft="0" placeholder="请输入RTMP媒体流地址">
				<up-button @click="submit(1)" style="width: 40rpx;" type="primary" size="mini" text="上传"></up-button>
			</fui-input>

			<fui-input :readonly="edabEditing" v-if="newlist.supervisionAlarmRtmp"
				v-model="newlist.supervisionAlarmRtmp" :padding="['20rpx','32rpx']" label="RTMP" :bottomLeft="0"
				placeholder="请输入RTMP媒体流地址">
				<up-button v-if="edabEditing" style="width: 40rpx;" @click="setData(1)" type="primary" size="mini"
					text="修改"></up-button>
				<up-button @click="submit(1)" v-if="!edabEditing" style="width: 40rpx;" type="primary" size="mini"
					text="上传"></up-button>
			</fui-input>

			<br />
			<fui-section title="故障报警媒体流地址" isLine>
				<template v-slot:right>
					<fui-icon @click="warning" :size="30" color="#FF2B2B" name="info"></fui-icon>
				</template>
			</fui-section>
			<fui-input v-if="!newlist.faultAlarmRtmp" v-model="faultAlarmRtmp" :padding="['20rpx','32rpx']" label="RTMP"
				:bottomLeft="0" placeholder="请输入RTMP媒体流地址">
				<up-button @click="submit(2)" style="width: 40rpx;" type="primary" size="mini" text="上传"></up-button>
			</fui-input>

			<fui-input :readonly="edabEditing1" v-if="newlist.faultAlarmRtmp" v-model="newlist.faultAlarmRtmp"
				:padding="['20rpx','32rpx']" label="RTMP" :bottomLeft="0" placeholder="请输入RTMP媒体流地址">
				<up-button v-if="edabEditing1" @click="edabEditing1 = !edabEditing1" style="width: 40rpx;"
					type="primary" size="mini" text="修改"></up-button>
				<up-button @click="submit(2)" v-if="!edabEditing1" style="width: 40rpx;" type="primary" size="mini"
					text="上传"></up-button>
			</fui-input>
			<br />

			<fui-section title="火灾报警视频流地址" isLine>
				<template v-slot:right>
					<fui-icon @click="warning" :size="30" color="#FF2B2B" name="info"></fui-icon>
				</template>
			</fui-section>
			<fui-input v-if="!newlist.fireAlarmRtmp" v-model="fireAlarmRtmp" :padding="['20rpx','32rpx']" label="RTMP"
				:bottomLeft="0" placeholder="请输入RTMP媒体流地址">
				<up-button @click="submit(3)" style="width: 40rpx;" type="primary" size="mini" text="上传"></up-button>
			</fui-input>

			<fui-input :readonly="edabEditing2" v-if="newlist.fireAlarmRtmp" v-model="newlist.fireAlarmRtmp"
				:padding="['20rpx','32rpx']" label="RTMP" :bottomLeft="0" placeholder="请输入RTMP媒体流地址">
				<up-button v-if="edabEditing2" @click="edabEditing2 = !edabEditing2" style="width: 40rpx;" type="primary"
					size="mini" text="修改"></up-button>
				<up-button @click="submit(3)" v-if="!edabEditing2" style="width: 40rpx;" type="primary" size="mini"
					text="上传"></up-button>
			</fui-input>

		</view>

	</view>
</template>

<script>
	import {
		BASE_IP
	} from '@/common/http/url.js'

	export default {
		name: "fireEarlyWarning",
		data() {
			return {
				user: "",
				supervisionAlarmRtmp: "", // 监管报警媒体流地址（rtmp
				fireAlarmRtmp: "", //火灾报警视频流地址（rtmp）
				newlist: {
					supervisionAlarmRtmp: "",
					faultAlarmRtmp: "",
					fireAlarmRtmp: ""
				},
				edabEditing: true,
				edabEditing1: true,
				edabEditing2: true,
				cid: ""
			}
		},
		methods: {
			setData(id) {
				if (id == 1) {
					this.newlist.supervisionAlarmRtmp = ""
					this.edabEditing = !this.edabEditing
				} else if (id == 2) {
					this.newlist.faultAlarmRtmp = ""
					this.edabEditing1 = !this.edabEditing1
				} else if (id == 3) {
					this.newlist.fireAlarmRtmp = ""
					this.edabEditing2 = !this.edabEditing2
				}
			},
			init() {
				let _tihs = this
				let url = BASE_IP + "pc/iotFireAlarmRecordRtmp/query/" + this.user.unitId
				uni.request({
					header: {
						"Authorization": this.user.token
					},
					url: url,
					success: (res) => {
						let fireEarlyWarning = {
							supervisionAlarmRtmp: res.data.supervisionAlarmRtmp,
							faultAlarmRtmp: res.data.faultAlarmRtmp,
							fireAlarmRtmp: res.data.fireAlarmRtmp
						}
						uni.setStorageSync("fireEarlyWarning", fireEarlyWarning)

						// console.log(res.supervisionAlarmRtmp)
					}
				})
				let fireEarlyWarning = uni.getStorageSync("fireEarlyWarning")
				this.newlist = fireEarlyWarning
			},
			warning() {
				uni.showModal({
					title: "提示",
					content: "添加前请测试播放RTMP媒体流确保生效",
					showCancel: false
				})
			},
			submit(id) {
				console.log(id)
				let cid = this.cid
				let url = ""
				if (id == 1) {
					let supervisionAlarmRtmp = this.supervisionAlarmRtmp
					console.log(supervisionAlarmRtmp)
					console.log(id)
					if (!supervisionAlarmRtmp) {
						uni.showToast({
							title: "未输入",
							icon: "error"
						})
						return
					}

					const regex = /^rtmp:\/\//;
					let flag = regex.test(supervisionAlarmRtmp)
					if (!flag) {
						uni.showToast({
							title: "地址不合法",
							icon: "error"
						})
						return
					}
					url = BASE_IP + "pc/iotFireAlarmRecordRtmp/updata/supervisionAlarmRtmp/" + this.user.unitId + "/" + cid
					this.edabEditing = !this.edabEditing
					this.req(url, supervisionAlarmRtmp)
					
				}

				if (id == 2) {
					let faultAlarmRtmp = this.faultAlarmRtmp
					if (!faultAlarmRtmp) {
						uni.showToast({
							title: "未输入",
							icon: "error"
						})
						return
					}

					const regex = /^rtmp:\/\//;
					let flag = regex.test(faultAlarmRtmp)
					if (!flag) {
						uni.showToast({
							title: "地址不合法",
							icon: "error"
						})
						return
					}
					url = BASE_IP + "pc/iotFireAlarmRecordRtmp/updata/faultAlarmRtmp/" + this.user.unitId + "/" + cid
					this.edabEditing1 = !this.edabEditing1
					this.req(url, faultAlarmRtmp)
				}

				if (id == 3) {
					let fireAlarmRtmp = this.fireAlarmRtmp
					console.log(fireAlarmRtmp)
					console.log(id)
					if (!fireAlarmRtmp) {
						uni.showToast({
							title: "未输入",
							icon: "error"
						})
						return
					}

					const regex = /^rtmp:\/\//;
					let flag = regex.test(fireAlarmRtmp)
					if (!flag) {
						uni.showToast({
							title: "地址不合法",
							icon: "error"
						})
						return
					}
					url = BASE_IP + "pc/iotFireAlarmRecordRtmp/updata/fireAlarmRtmp/" + this.user.unitId + "/" + cid
					this.edabEditing2 = !this.edabEditing2
					this.req(url, fireAlarmRtmp)
					
				}
			},
			req(url, live) {
				console.log(url)
				uni.request({
					header: {
						"Authorization": this.user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					url: url,
					data: {
						live: live
					},
					method: "POST",
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200) {
							uni.showToast({
								title: "提交成功",
								icon: "success"
							})
							setTimeout(()=>{
								this.init()
							},500)
							
							setTimeout(()=>{
								this.init()
							},800)
						}
						if (res.statusCode == 404) {
							uni.showToast({
								title: "提交失败",
								icon: "error"
							})
						}
						if (res.statusCode == 500) {
							uni.showToast({
								title: "提交失败",
								icon: "error"
							})
						}
					},
					fail: () => {
						uni.showToast({
							title: "提交失败",
							icon: "error"
						})
					}
				})
			}
		},
		onLoad() {
			setTimeout(()=>{
				this.init()
			},500)
			
			setTimeout(()=>{
				this.init()
			},800)
			
			let user = uni.getStorageSync("user")
			let cid = uni.getStorageSync("cid")
			this.cid = cid
			this.user = user
			console.log(user)
			
			let fireEarlyWarning = uni.getStorageSync("fireEarlyWarning")
			this.newlist = fireEarlyWarning
			console.log(this.newlist)
		}
	}
</script>

<style>
	.fires {
		width: 98%;
		height: auto;
	}
</style>